import { request } from './request'

// 用户相关接口类型定义
export interface User {
  id: number
  username: string
  email: string
  avatar?: string
}

export interface LoginParams {
  username: string
  password: string
}

export interface LoginResponse {
  token: string
  user: User
}

// 用户相关 API
export const userApi = {
  /**
   * 用户登录
   */
  login(params: LoginParams) {
    return request.post<LoginResponse>('/user/login', params)
  },

  /**
   * 获取用户信息
   */
  getUserInfo() {
    return request.get<User>('/user/info')
  },

  /**
   * 更新用户信息
   */
  updateUserInfo(data: Partial<User>) {
    return request.put<User>('/user/info', data)
  },

  /**
   * 用户登出
   */
  logout() {
    return request.post('/user/logout')
  },
}

// 文章相关接口类型定义
export interface Article {
  id: number
  title: string
  content: string
  author: string
  createTime: string
  updateTime: string
}

export interface ArticleListParams {
  page: number
  pageSize: number
  keyword?: string
}

export interface ArticleListResponse {
  list: Article[]
  total: number
}

// 文章相关 API
export const articleApi = {
  /**
   * 获取文章列表
   */
  getList(params: ArticleListParams) {
    return request.get<ArticleListResponse>('/article/list', { params })
  },

  /**
   * 获取文章详情
   */
  getDetail(id: number) {
    return request.get<Article>(`/article/${id}`)
  },

  /**
   * 创建文章
   */
  create(data: Partial<Article>) {
    return request.post<Article>('/article', data)
  },

  /**
   * 更新文章
   */
  update(id: number, data: Partial<Article>) {
    return request.put<Article>(`/article/${id}`, data)
  },

  /**
   * 删除文章
   */
  delete(id: number) {
    return request.delete(`/article/${id}`)
  },
}
